//Team 4 - Gold Coast
//Duy Kha Nguyen 598240 
//Rongkai Zhou 562946 
//Zhangyue Cheng 548692
//Binzan Liang 591911
//Wei Liu 572322
package twitter;

import java.io.IOException;
import java.util.Properties;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

import org.apache.log4j.Logger;
import org.lightcouch.CouchDbClient;

import com.fasterxml.jackson.databind.ObjectMapper;

public class TweetCollectApp {

    private static final Logger logger = Logger.getLogger(TweetCollectApp.class);

    public static void main(String[] args) {
        final Properties properties = new Properties();
        try {
            properties.load(TweetCollectApp.class.getClassLoader().getResourceAsStream("config.properties"));
        } catch (IOException ex) {
            logger.error("Properties file not found");
            System.exit(-1);
        }
        final String consumerKey = properties.getProperty("consumerKey");
        final String consumerSecret = properties.getProperty("consumerSecret");
        final String token = properties.getProperty("token");
        final String tokenSecret = properties.getProperty("tokenSecret");
        final double swLatitude = Double.valueOf(properties.getProperty("swLatitude"));
        final double swLongitude = Double.valueOf(properties.getProperty("swLongitude"));
        final double neLatitude = Double.valueOf(properties.getProperty("neLatitude"));
        final double neLongitude = Double.valueOf(properties.getProperty("neLongitude"));

        final CouchDbClient couchDb = new CouchDbClient("couchdb.properties");
        final ObjectMapper jsonParser = new ObjectMapper();
        final BlockingQueue<String> tweetQueue = new LinkedBlockingQueue<String>(100000);

        final Thread streamTweetThread = new Thread(new CollectTweetTask(tweetQueue, consumerKey, consumerSecret, token, tokenSecret, swLatitude, swLongitude,
                neLatitude, neLongitude));
        streamTweetThread.start();

        final Thread storeTweetThread1 = new Thread(new StoreTweetTask(tweetQueue, couchDb, jsonParser));
        storeTweetThread1.start();

        final Thread storeTweetThread2 = new Thread(new StoreTweetTask(tweetQueue, couchDb, jsonParser));
        storeTweetThread2.start();

        final Thread storeTweetThread3 = new Thread(new StoreTweetTask(tweetQueue, couchDb, jsonParser));
        storeTweetThread3.start();

        try {
            streamTweetThread.join();
        } catch (InterruptedException e) {
            logger.error("Error when waiting fror streamTweetThread", e);
        }
    }
}
